<template>
	<view>
		<!--<z-nav-bar>选择紧急联系人</z-nav-bar>
		<public-module></public-module>-->
		<view class="notice-box">
			<uni-notice-bar single :text="tips"></uni-notice-bar>
		</view>
		<view class="list-content">
			<template v-for="(item, index) in eduStudentUrgentList ">
				<view class="urgent" :key="item.cellNo" @click="queryDetail('edit',item)">
					<view class="name">{{item.urgentName}}
						<text class="urgentType" v-if="false">{{item.urgentType}}</text>
						<text class="urgentType">{{getTypeName(item.urgentType)}}</text>
					</view>
					<view class="phone">{{item.cellNo}}
						<uni-icons type="arrowright" size="16" color="#969696"></uni-icons>
					</view>
					<view class="close" @tap.stop="deleteItem(index)">
						<icon type="clear" size="16"/>
					</view>
				</view>
			</template>
		</view>
		<button type="primary" class="btn" @click="queryDetail('add')">添加联系人</button>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				tips: '请填写您的家人、朋友联系方式',
				relationTypeOptions: [], // 类别list
				eduStudentUrgentList: [/*{
					urgentName: '许振涛',
					cellNo: '13452872454',
					urgentType: '父亲'
				}, {
					urgentName: '吴霞',
					cellNo: '123123213123',
					urgentType: '母亲'
				}*/],
				tmpUrgent: null,
				tmpItem: null,
				type: null // 操作类别
			}
		},
		onLoad(option) {
			const data = JSON.parse(decodeURIComponent(option.data));
			this.eduStudentUrgentList = data;
			//console.log(JSON.stringify(this.eduStudentUrgentList))
			
			this.$api.common.getDict("edu_urgent_type").then(res => {
				this.relationTypeOptions = res.data
			})
		},
		onShow() {
			if('添加' === this.type) {
				this.eduStudentUrgentList.push(this.tmpUrgent)
			} else {
				this.eduStudentUrgentList.forEach(ele => {
					if(ele.urgentName===this.tmpItem.urgentName 
						&& ele.cellNo===this.tmpItem.cellNo
						&&ele.urgentType===this.tmpItem.urgentType) {
							
						ele.urgentType = this.tmpUrgent.urgentType
						ele.cellNo = this.tmpUrgent.cellNo
						ele.urgentName = this.tmpUrgent.urgentName
					}
				})
			}
			
			// 返回前面数据
			let pages = getCurrentPages();  //获取所有页面栈实例列表
			//let nowPage = pages[ pages.length - 1];  //当前页页面实例
			let prevPage = pages[ pages.length - 2 ];  //上一页页面实例
			prevPage.$vm.tmpUrgentList = this.eduStudentUrgentList
		},
		methods: {
			deleteItem (i) {
				this.eduStudentUrgentList.splice(i, 1)
			},
			queryDetail(type,item) {
				this.tmpItem = item
				let data = encodeURIComponent(JSON.stringify(item));
				uni.navigateTo({
					url: `/subpages/home/addStudentUrgent?type=${type}&data=${data}`
				});
			},
			getTypeName(type) {
				let currentOption = []
				this.relationTypeOptions.filter(option => {
					if(option.dictValue.toString() === type.toString()) {
						currentOption = option
					}
				})
				
				return currentOption.dictLabel
			}
		},

	}
</script>

<style lang="scss">
	@import '@/style/mixin.scss';

	page {
		background-color: $uni-bg-color-grey;
	}
	.btn{
		position: fixed;
		bottom:30px;
		left:0;
		right:0;
		margin-left:auto;
		margin-right:auto;
		width: 686upx;
		background-color: $uni-theme-color;
		border-radius: 30px;
	}
	.list-content {
		margin-bottom: 100px;
		overflow-y: auto;
		height: 100vh;
	}
	.urgent {
		position: relative;
		padding: 20px;
		width: 686upx;
		border-top-left-radius: 16px;
		border-bottom-left-radius: 16px;
		border-bottom-right-radius: 16px;
		background: #fff;
		margin: 0 auto;
		margin-top: 16px;
		display: flex;
		justify-content: space-between;
		
		.name {
			color: #323232;
			font-size: 17px;

			.urgentType {
				margin-left: 10px;
				font-size: 15px;
				color: #969696;
			}
		}

		.phone {
			font-size: 15px;
			color: #969696;
		}
		
		.close {
			position: absolute;
			top: 4px;
			right: 4px;
		}
	}
</style>
